package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.BehaviorConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/*
kafka消费者 ,监听broker中的消息,对app端文章表 ap_article的likes/collection /comment / views 进行更新
 */
@Slf4j
@Component
public class ArticleBehaviorListener {
    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = BehaviorConstants.USER_BEHAVIOR_TOPIC)
    public void onMessage(String msg) {
        if (StringUtils.isNoneBlank(msg)) {
            log.info("收到用户行文相关的kafka消息:{},开始更新ap_article...", msg);

            Map map = JSON.parseObject(msg, Map.class);

            apArticleService.updateArticleByMap(map);

            log.info("收到用户行文相关的kafka消息,更新ap_article成功...");
        }
    }
}
